package com.kadron.application;

import java.io.Serializable;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.creator.KApplicationModelCreator;
import com.kadron.dao.KApplicationDAO;
import com.kadron.dao.KImageDAO;
import com.kadron.datamodel.KApplicationModelDataProvider;
import com.kadron.datamodel.KDataModel;
import com.kadron.entity.Application;
import com.kadron.manager.KMessageManager;
import com.kadron.manager.KSelection;

@AutoCreate
@Scope(ScopeType.PAGE)
@Name("kApplicationListManager")
public class KApplicationListManager extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3111988099407754250L;
	
	private KDataModel applicationList;
	private KApplicationModelDataProvider dataProvider;
	
	@In
	private KApplicationDAO applicationDAO;
	
	@In
	private KImageDAO imageDAO;
	
	@Create
	public void init() {
		dataProvider = new KApplicationModelDataProvider(new KApplicationModelCreator(imageDAO, applicationDAO), applicationDAO);
		applicationList = new KDataModel(dataProvider);
	}

	public KDataModel getApplicationList() {
		return applicationList;
	}
	
	public String edit(Long id) {
		if (id == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		KSelection kselection = (KSelection) Component.getInstance("kSelection");
		kselection.setItemId(id);
		return "e_application";
	}
	
	public void delete(Application application) {
		if (application == null) {
			bad(KConst.BAD_THING);
			return;
		}
		applicationList.remove(application);
		applicationDAO.delete(application);
	}
}
